﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace MTRC.ODMS.Web.SOM.PassengerFlow
{
    public partial class PassengerFlowEnquiry : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M141001";
            }
        }

        #endregion

        #region PageEvent

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.grdSearchResult.PageSize = this.GridViewPageDefaultSize();

                ucPsc.MenuID = this.MenuID;
                ucPsc.BindUcPsc(this.MenuID, "M141001_F2", "M141001_F3");
                FunctionItemSetting();
                SetLanguage();
            }
        }

        void SetLanguage_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                FunctionItemSetting();
            }
            SetLanguage();
        }

        private void SetLanguage()
        {
            ucPsc.MenuID = this.MenuID;
            ucPsc.BindUcPsc(this.MenuID, "M141001_F2", "M141001_F3");

            btnExport.Text = GetLabel("SOM_PsgExport");
            btnNew.Text = GetLabel("SOM_PsgAddNew");
            btnSearch.Text = GetLabel("SOM_PsgSearch");
            
            //Bind Grid
            BindGrid();
        }

        private void FunctionItemSetting()
        {
            //Add New Button ReadOnly Or Not
            btnNew.Enabled = !DAL.Security.User.CanFunctionItemAccess("M141001_F1", this.ODMUser.UserProfileID, this.MenuID);
            
            //Access Station
        }

        #endregion

        #region GridView Event

        private void BindGrid()
        {
            /* Comment by jack 20150824 for u70 */
            if (DAL.Security.User.CanFunctionItemAccess("M141001_F2", this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {

            }
            else if (DAL.Security.User.CanFunctionItemAccess("M141001_F3", this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {

            }
            else
            {
                //update by gordon 20151024 U248
                //if (!IsValidStation(this.ODMUser.LocationID, true))
                if (!IsValidStation(this.ODMUser.LocationID))
                {
                    if (Request.QueryString["IsReject"] != "1")
                    {
                        this.PAlert(this.GetMessage("SOM_NotValidStation"));
                    }
                    btnExport.Enabled = false;
                    DataTable dtNull = new DataTable();
                    this.grdSearchResult.DataSource = dtNull.DefaultView;
                    this.grdSearchResult.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);
                    this.grdSearchResult.DataBind();
                    dtNull.Dispose();
                    return;
                }

            }
            /************************************/
          
            DataTable dt = GetData();
            btnExport.Enabled = dt.Rows.Count > 0;
            grdSearchResult.DataSource = dt.DefaultView;
            grdSearchResult.DataBind();
        }

        private DataTable GetData()
        {
            DateTime from = this.ucPsc.GetFromDateFromUcPsc();
            DateTime to = this.ucPsc.GetToDateFromUcPsc();

            //Begin  liuwenjing add 2015-09-30 issue:U224
            VerificationDate(from, to);
            //End
            string locationIds = this.ucPsc.GetLocationIDFromUcPsc();

            return BLL.SOM.PassengerFlow.PassengerFlowBLL.GetPassengerFlowData(from, to, locationIds, this.LangCode);
        }

        protected void grdSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
        }

        protected void grdSearchResult_NeedDataBind()
        {
            BindGrid();
        }
        #endregion

        #region Button Event
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                BindGrid();
            }
            finally
            {
                //add by gordon 20150812 | For Search Wait Cursor
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "", "showWait('');", true);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //string templatePath = Server.MapPath("~")+"\\Template\\MTRC\\PassengerFlow Template.xlsx";
            string templateFilePath = APJ.Util.Utility.GetAppSetting("SOM_ExportExcelTemplatePath");
            if (templateFilePath.LastIndexOf("\\") != templateFilePath.Length - 1)
                templateFilePath += "\\";
            string templatePath = templateFilePath + "PassengerFlow Template.xlsx";//add by jack 20160127 ISU22 fixed for same export file name
            //string exportPath = Server.MapPath("~") + "\\Template\\MTRC\\PassengerFlow.xlsx";
            string ExcelDownloadFilePath = APJ.Util.Utility.GetAppSetting("SOM_ExportExcelTempFilePath");
            if (ExcelDownloadFilePath.LastIndexOf("\\") != ExcelDownloadFilePath.Length - 1)
                ExcelDownloadFilePath += "\\";
            string exportPath = ExcelDownloadFilePath + "PassengerFlow" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";//add by jack 20160127 ISU22 fixed for same export file name

            DataTable dt = GetData();
            dt.Columns.RemoveAt(dt.Columns.Count - 1);
            dt.Columns.RemoveAt(dt.Columns.Count - 1);

            dt.Columns[0].ColumnName = this.GetLabel("SOM_PsgOperatingDay");
            dt.Columns[1].ColumnName = this.GetLabel("SOM_PsgStation");
            dt.Columns[2].ColumnName = this.GetLabel("SOM_PsgDescription");
            dt.Columns[3].ColumnName = this.GetLabel("SOM_PsgIncoming");
            dt.Columns[4].ColumnName = this.GetLabel("SOM_PsgInEst")+" ";
            dt.Columns[5].ColumnName = this.GetLabel("SOM_PsgOutgoing");
            dt.Columns[6].ColumnName = this.GetLabel("SOM_PsgOutEst") + "  ";
            dt.Columns[7].ColumnName = this.GetLabel("SOM_PsgTSTTransit");
            dt.Columns[8].ColumnName = this.GetLabel("SOM_PsgFlagTSTTransit") + "   ";
            dt.Columns[9].ColumnName = this.GetLabel("SOM_PsgETSTransit");
            dt.Columns[10].ColumnName = this.GetLabel("SOM_PsgFlagETSTransit") + "    ";
            dt.Columns[11].ColumnName = this.GetLabel("SOM_PsgLineCode");
            dt.Columns[12].ColumnName = this.GetLabel("SOM_PsgLocationCode");
            dt.Columns[13].ColumnName = this.GetLabel("SOM_PsgFromLocationCode");
            dt.Columns[14].ColumnName = this.GetLabel("SOM_PsgLastUpdUser");
            dt.Columns[15].ColumnName = this.GetLabel("SOM_PsgLastUpdTime");

            //update by gordon 20150910
            //CreateExcelFile.ExportExcelWithTemplate(dt,templatePath,exportPath,Response,"PassengerFlow");
            CreateExcelFile.ExportExcelWithTemplate(dt, templatePath, exportPath, Response,Page, "PassengerFlow");

        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("PassengerFlowEntry.aspx");
        }
        //Begin  liuwenjing add 2015-09-30 issue:U224
        public void VerificationDate(DateTime from, DateTime to)
        {
          
            if (from.ToString().Length > 0 && to.ToString().Length > 0)
            {
                //if (InvalidateInputDate(from.ToString(), to.ToString()))
                if (InvalidateInputDate(from, to))
                {
                    this.PAlert(this.GetMessage("COM_StartEndDate"));
                    return ;
                }
            }
        }
       //End
        #endregion
    }
}